axios.defaults.baseURL = 'http://localhost:8080';
axios.interceptors.request.use(function (config) {
    var token = localStorage.getItem("token");
    if (token != null) {
        config.headers.common['Authorization'] = "Bearer " + token;

    } else {
        config.headers.common['Authorization'] = null;
    }
    return config;
});
axios.interceptors.response.use(function (res) {
    return res.data
}, err => {
    console.log(err);
    return Promise.resolve(err);
});

var $api = {
    setToken(token) {
        localStorage.setItem("token", token);
    },
    clearToken() {
        localStorage.removeItem("token");
        axios.defaults.headers.common['Authorization'] = "";
    },
    login(username, password) {
        return axios({
            method: 'post',
            url: '/sys/login',
            params: {
                username: username,
                password: password
            }
        });
    },
};